package com.xtz.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.xtz.domain.ShoppingCart;
import com.xtz.dto.ShoppingCartDto;
import com.xtz.dto.SubCartDto;
import com.xtz.mapper.ShoppingCartMapper;
import com.xtz.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * date: 2022/5/14 17:15
 *
 * @author xietianze
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public Boolean add(ShoppingCartDto dto) {

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);

        String idStr = new Snowflake().nextIdStr();
        shoppingCart.setId(idStr);
        shoppingCart.setUserId("1");
        shoppingCart.setSetmealId("null");
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(new Date());
        shoppingCart.setCreateUser("1");
        shoppingCart.setUpdateTime(new Date());
        shoppingCart.setUpdateUser("1");

        return shoppingCartMapper.add(shoppingCart)>0;
    }

    /**
     * 获得列表
     */
    @Override
    public List<ShoppingCart> getList() {
        return shoppingCartMapper.getList();
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @Override
    public Boolean cleanCart() {
        return shoppingCartMapper.cleanCart()>0;
    }

    /**
     * 减少
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean sub(SubCartDto dto) {


        return shoppingCartMapper.sub(dto) > 0;
    }


}
